package com.graduation.mathematics.ui.activity;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.graduation.mathematics.R;
import com.graduation.mathematics.base.BaseActivity;
import com.graduation.mathematics.bean.UserInfo;
import com.lxj.xpopup.XPopup;

import org.litepal.LitePal;

/**
 * @ProjectName: mathematicsWorkbook
 * @Package: com.graduation.mathematics.ui.activity
 * @ClassName: RegisterActivity
 * @Description: java类作用描述
 * @Author: APC
 * @CreateDate: 2021/2/23 15:07
 * @Version: 1.0
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_identity;
    private TextView tv_class;
    private Button btnRegister;
    private Button btnCancel;

    private EditText etAccount;
    private EditText etPassword;
    private EditText etPasswordConfirm;
    private EditText etUserName;
    private TextView labelStudent;
    private EditText etStudent;

    private int identity = 0;
    private String myClass = "全校所有班级";

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initDataAndEvent() {
        initView();
    }

    private void initView(){
        etAccount = findView(R.id.et_account);
        etPassword = findView(R.id.et_password_login_fragment);
        etPasswordConfirm = findView(R.id.et_password_login_fragment_confirm);
        etUserName = findView(R.id.et_username);
        labelStudent = findView(R.id.label_student_account);
        etStudent = findView(R.id.et_student_account);
        labelStudent.setVisibility(View.GONE);
        etStudent.setVisibility(View.GONE);

        btnRegister = findView(R.id.btn_login_register_fragment);
        btnCancel = findView(R.id.btn_login_register_cancel);
        btnRegister.setOnClickListener(this);
        btnCancel.setOnClickListener(this);


        tv_identity = findView(R.id.tv_identity);
        tv_class = findView(R.id.tv_class);
        tv_identity.setOnClickListener(this);
        tv_class.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_identity:
                showIdentityChoose();
                break;
            case R.id.tv_class:
                showClassChoose();
                break;
            case R.id.btn_login_register_fragment:
                registerAndGoLogin();
                break;
            case R.id.btn_login_register_cancel:
                finish();
                break;
            default:
                break;
        }
    }

    private void registerAndGoLogin(){
        String account = etAccount.getText().toString().trim();
        String username = etUserName.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String pwdConfirm = etPasswordConfirm.getText().toString().trim();
        String studetAccount = "";
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort("请输入帐号！");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort("请输入密码！");
            return;
        }
        if (password.length() != 6) {
            ToastUtils.showShort("密码长度不正确！");
            return;
        }
        if (TextUtils.isEmpty(pwdConfirm)) {
            ToastUtils.showShort("请确认密码！");
            return;
        }
        if (!password.equals(pwdConfirm)) {
            ToastUtils.showShort("两次密码不一样！");
            return;
        }

        if (identity == 1) {
            studetAccount = etStudent.getText().toString().trim();
            if (TextUtils.isEmpty(studetAccount) || !LitePal.isExist(UserInfo.class, "account = ? ", studetAccount)) {
                ToastUtils.showShort("请输入对应学生账号");
                return;
            }
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setAccount(account);
        userInfo.setName(username);
        userInfo.setPassword(password);
        userInfo.setGrade(myClass);
        userInfo.setRileId(identity);
        userInfo.setStudentAccount(studetAccount);

        if (LitePal.isExist(UserInfo.class, "account = ? ", account)) {
            ToastUtils.showShort("用户已存在！");
            return;
        }

        if (userInfo.save()) {
            SPUtils.getInstance().put("account", account);
            SPUtils.getInstance().put("password", password);
            setResult(1002);
            finish();
        } else {
            ToastUtils.showShort("注册失败！");
            return;
        }

    }

    private void showIdentityChoose(){
        new XPopup.Builder(this)
                .isDestroyOnDismiss(true)
                .asCenterList("选择身份", getResources().getStringArray(R.array.roleNames),
                        (position, text) -> {
                            identity = position;
                            tv_identity.setText(text);
                            if (identity == 1) {    //家长注册需输入对应学生账号
                                labelStudent.setVisibility(View.VISIBLE);
                                etStudent.setVisibility(View.VISIBLE);
                            } else {        //学生注册不需输入对应学生账号
                                labelStudent.setVisibility(View.GONE);
                                etStudent.setVisibility(View.GONE);
                            }
                        }).show();

    }

    private void showClassChoose(){
        new XPopup.Builder(this)
                .isDestroyOnDismiss(true)
                .asCenterList("选择班级", getResources().getStringArray(R.array.gradeNames),
                        (position, text) -> {
                            myClass = text;
                            tv_class.setText(text);
                        }).show();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);

    }
}
